﻿
//Function, extends the default Function object.
LightScript.using("LightScript.Core", function()
{
    var thisClass = LightScript.Core.createClass("LightScript.Function");

    //# Private Members

    //# Constructions

    //# Public Members

    //# Private Methods

    //# Public Methods
    // Creates a delegate for the method.
    //e.g: createDelegate(foo, this, [arg1, arg2, arg3, ...])
    thisClass.createDelegate = createDelegate;
    function createDelegate(method, instance, args, overrideArgsFromArguments, onlyOverrideUndefinedValue)
    {
        if (overrideArgsFromArguments)
        {
            return function()
            {
                for (var i = 0, len = arguments.length; i < len; i++)
                {
                    if (onlyOverrideUndefinedValue && args[i] !== undefined) continue;
                    args[i] = arguments[i];
                }
                method.apply(instance, args);
            };
        }
        else
            return function() { method.apply(instance, args || []); };
    }

    // Creates a callback for the method.
    //e.g: createCallback(foo, [arg1, arg2, arg3, ...]);
    thisClass.createCallback = createCallback;
    function createCallback(method, args, overrideArgsFromArguments, onlyOverrideUndefinedValue)
    {
        return createDelegate(method, method, args, overrideArgsFromArguments, onlyOverrideUndefinedValue);
    }

    //# Shortcuts
});
